/*
 * File: Fu_HallSensCalc.c
 *
 * Code generated for Simulink model 'Fu_HallSensCalc'.
 *
 * Model version                  : 1.715
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sun Jun 16 12:53:08 2013
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Fu_HallSensCalc.h"
#include "Fu_HallSensCalc_private.h"

/* Named constants for Chart: '<S1>/Fu_HallSensCalc_Plaus' */
#define Fu_HallSensCalc_IN_INIT        ((uint8_T)1U)
#define Fu_HallSensCalc_IN_ST0         ((uint8_T)2U)
#define Fu_HallSensCalc_IN_ST1         ((uint8_T)3U)
#define Fu_HallSensCalc_IN_ST2         ((uint8_T)4U)
#define Fu_HallSensCalc_IN_ST3         ((uint8_T)5U)
#define Fu_HallSensCalc_IN_ST4         ((uint8_T)6U)
#define Fu_HallSensCalc_IN_ST5         ((uint8_T)7U)

/* Exported block signals */
uint32_T HallCalc_TickDifAry[7];       /* '<S1>/Fu_HallSensCalc_SysTick' */
uint8_T HallCalc_Index;                /* '<S1>/Bitwise Operator1' */
uint8_T HallCalc_IndexPlaus;           /* '<S1>/Fu_HallSensCalc_Plaus' */

/* Block signals (auto storage) */
BlockIO_Fu_HallSensCalc Fu_HallSensCalc_B;

/* Block states (auto storage) */
D_Work_Fu_HallSensCalc Fu_HallSensCalc_DWork;

/* Previous zero-crossings (trigger) states */
PrevZCSigStates_Fu_HallSensCalc Fu_HallSensCalc_PrevZCSigState;

/* Real-time model */
RT_MODEL_Fu_HallSensCalc Fu_HallSensCalc_M_;
RT_MODEL_Fu_HallSensCalc *const Fu_HallSensCalc_M = &Fu_HallSensCalc_M_;

/* Model step function */
void Fu_HallSensCalc_step(void)
{
  uint32_T qY;
  uint32_T qY_0;

  /* S-Function (sfix_bitop): '<S1>/Bitwise Operator1' incorporates:
   *  ArithShift: '<S1>/Shift Arithmetic'
   *  ArithShift: '<S1>/Shift Arithmetic1'
   *  DataTypeConversion: '<S1>/Data Type Conversion'
   *  DataTypeConversion: '<S1>/Data Type Conversion1'
   *  DataTypeConversion: '<S1>/Data Type Conversion2'
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   *  Inport: '<Root>/In6'
   */
  HallCalc_Index = (uint8_T)((int32_T)Net_FlgHallSens2 << 1 | (int32_T)
    Net_FlgHallSens1 | (int32_T)Net_FlgHallSens3 << 2);

  /* Chart: '<S1>/Fu_HallSensCalc_Plaus' */
  /* Gateway: Fu_HallSensCalc/Fu_HallSensCalc_Plaus */
  /* During: Fu_HallSensCalc/Fu_HallSensCalc_Plaus */
  if (Fu_HallSensCalc_DWork.is_active_c2_Fu_HallSensCalc == 0) {
    /* Entry: Fu_HallSensCalc/Fu_HallSensCalc_Plaus */
    Fu_HallSensCalc_DWork.is_active_c2_Fu_HallSensCalc = 1U;

    /* Entry Internal: Fu_HallSensCalc/Fu_HallSensCalc_Plaus */
    /* Transition: '<S2>:31' */
    Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_INIT;

    /* Entry 'INIT': '<S2>:27' */
    Fu_HallSensCalc_DWork.StHardware[0] = 1U;
    Fu_HallSensCalc_DWork.StHardware[1] = 3U;
    Fu_HallSensCalc_DWork.StHardware[2] = 2U;
    Fu_HallSensCalc_DWork.StHardware[3] = 6U;
    Fu_HallSensCalc_DWork.StHardware[4] = 4U;
    Fu_HallSensCalc_DWork.StHardware[5] = 5U;
    Fu_HallSensCalc_B.PlausTogBit = FALSE;
    HallCalc_IndexPlaus = HallCalc_Index;
  } else {
    switch (Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc) {
     case Fu_HallSensCalc_IN_INIT:
      /* During 'INIT': '<S2>:27' */
      /* Transition: '<S2>:34' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[0]) {
        /* Transition: '<S2>:11' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST0;

        /* Entry 'ST0': '<S2>:3' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[0];
      } else {
        /* Transition: '<S2>:22' */
        if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[1]) {
          /* Transition: '<S2>:13' */
          Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST1;

          /* Entry 'ST1': '<S2>:4' */
          Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
          HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[1];
        } else {
          /* Transition: '<S2>:23' */
          if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[2]) {
            /* Transition: '<S2>:15' */
            Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST2;

            /* Entry 'ST2': '<S2>:5' */
            Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
            HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[2];
          } else {
            /* Transition: '<S2>:24' */
            if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[3]) {
              /* Transition: '<S2>:17' */
              Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc =
                Fu_HallSensCalc_IN_ST3;

              /* Entry 'ST3': '<S2>:6' */
              Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
              HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[3];
            } else {
              /* Transition: '<S2>:25' */
              if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[4]) {
                /* Transition: '<S2>:19' */
                Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc =
                  Fu_HallSensCalc_IN_ST4;

                /* Entry 'ST4': '<S2>:7' */
                Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
                HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[4];
              } else {
                /* Transition: '<S2>:26' */
                if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[5]) {
                  /* Transition: '<S2>:21' */
                  Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc =
                    Fu_HallSensCalc_IN_ST5;

                  /* Entry 'ST5': '<S2>:8' */
                  Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
                  HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[5];
                }
              }
            }
          }
        }
      }
      break;

     case Fu_HallSensCalc_IN_ST0:
      /* During 'ST0': '<S2>:3' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[1]) {
        /* Transition: '<S2>:38' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST1;

        /* Entry 'ST1': '<S2>:4' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[1];
      }
      break;

     case Fu_HallSensCalc_IN_ST1:
      /* During 'ST1': '<S2>:4' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[2]) {
        /* Transition: '<S2>:40' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST2;

        /* Entry 'ST2': '<S2>:5' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[2];
      }
      break;

     case Fu_HallSensCalc_IN_ST2:
      /* During 'ST2': '<S2>:5' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[3]) {
        /* Transition: '<S2>:42' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST3;

        /* Entry 'ST3': '<S2>:6' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[3];
      }
      break;

     case Fu_HallSensCalc_IN_ST3:
      /* During 'ST3': '<S2>:6' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[4]) {
        /* Transition: '<S2>:44' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST4;

        /* Entry 'ST4': '<S2>:7' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[4];
      }
      break;

     case Fu_HallSensCalc_IN_ST4:
      /* During 'ST4': '<S2>:7' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[5]) {
        /* Transition: '<S2>:46' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST5;

        /* Entry 'ST5': '<S2>:8' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[5];
      }
      break;

     default:
      /* During 'ST5': '<S2>:8' */
      /* Transition: '<S2>:48' */
      if (HallCalc_Index == Fu_HallSensCalc_DWork.StHardware[0]) {
        /* Transition: '<S2>:51' */
        /* Transition: '<S2>:50' */
        Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = Fu_HallSensCalc_IN_ST0;

        /* Entry 'ST0': '<S2>:3' */
        Fu_HallSensCalc_B.PlausTogBit = !Fu_HallSensCalc_B.PlausTogBit;
        HallCalc_IndexPlaus = Fu_HallSensCalc_DWork.StHardware[0];
      }
      break;
    }
  }

  /* End of Chart: '<S1>/Fu_HallSensCalc_Plaus' */

  /* Chart: '<S1>/Fu_HallSensCalc_SysTick' incorporates:
   *  TriggerPort: '<S3>/loc_CalcTrig'
   */
  if (((Fu_HallSensCalc_PrevZCSigState.Fu_HallSensCalc_SysTick_Trig_ZC ==
        POS_ZCSIG) != Fu_HallSensCalc_B.PlausTogBit) &&
      (Fu_HallSensCalc_PrevZCSigState.Fu_HallSensCalc_SysTick_Trig_ZC !=
       UNINITIALIZED_ZCSIG)) {
    /* Gateway: Fu_HallSensCalc/Fu_HallSensCalc_SysTick */
    /* Event: '<S3>:1' */
    /* During: Fu_HallSensCalc/Fu_HallSensCalc_SysTick */
    /* Entry Internal: Fu_HallSensCalc/Fu_HallSensCalc_SysTick */
    /* Transition: '<S3>:7' */
    if (Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index] < 16777217U) {
      /* Inport: '<Root>/In3' */
      /* Transition: '<S3>:9' */
      if (Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index] > Os_SysTick) {
        /* Transition: '<S3>:11' */
        qY_0 = Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index] - Os_SysTick;
        if (qY_0 > Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index]) {
          qY_0 = 0U;
        }

        HallCalc_TickDifAry[HallCalc_Index] = qY_0;

        /* Transition: '<S3>:16' */
      } else {
        /* Transition: '<S3>:13' */
        /* Transition: '<S3>:15' */
        qY_0 = Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index] + 16777215U;
        if (qY_0 < Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index]) {
          qY_0 = MAX_uint32_T;
        }

        qY = qY_0 - Os_SysTick;
        if (qY > qY_0) {
          qY = 0U;
        }

        HallCalc_TickDifAry[HallCalc_Index] = qY;
      }

      /* Transition: '<S3>:21' */
    } else {
      /* Transition: '<S3>:19' */
      /* Transition: '<S3>:20' */
    }

    /* Inport: '<Root>/In3' */
    /* Transition: '<S3>:23' */
    Fu_HallSensCalc_DWork.HallTickOld[HallCalc_Index] = Os_SysTick;
  }

  Fu_HallSensCalc_PrevZCSigState.Fu_HallSensCalc_SysTick_Trig_ZC = (uint8_T)
    (Fu_HallSensCalc_B.PlausTogBit ? (int32_T)POS_ZCSIG : (int32_T)ZERO_ZCSIG);
}

/* Model initialize function */
void Fu_HallSensCalc_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(Fu_HallSensCalc_M, (NULL));

  /* block I/O */
  (void) memset(((void *) &Fu_HallSensCalc_B), 0,
                sizeof(BlockIO_Fu_HallSensCalc));

  /* exported global signals */
  {
    int_T i;
    for (i = 0; i < 7; i++) {
      HallCalc_TickDifAry[i] = 0U;
    }
  }

  HallCalc_Index = 0U;
  HallCalc_IndexPlaus = 0U;

  /* states (dwork) */
  (void) memset((void *)&Fu_HallSensCalc_DWork, 0,
                sizeof(D_Work_Fu_HallSensCalc));
  Fu_HallSensCalc_PrevZCSigState.Fu_HallSensCalc_SysTick_Trig_ZC =
    UNINITIALIZED_ZCSIG;

  {
    int32_T i;

    /* InitializeConditions for Chart: '<S1>/Fu_HallSensCalc_Plaus' */
    Fu_HallSensCalc_DWork.is_active_c2_Fu_HallSensCalc = 0U;
    Fu_HallSensCalc_DWork.is_c2_Fu_HallSensCalc = 0U;
    for (i = 0; i < 6; i++) {
      Fu_HallSensCalc_DWork.StHardware[i] = 0U;
    }

    HallCalc_IndexPlaus = 0U;
    Fu_HallSensCalc_B.PlausTogBit = FALSE;

    /* End of InitializeConditions for Chart: '<S1>/Fu_HallSensCalc_Plaus' */

    /* InitializeConditions for Chart: '<S1>/Fu_HallSensCalc_SysTick' */
    Fu_HallSensCalc_DWork.HallTickOld[0] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[1] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[2] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[3] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[4] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[5] = 16777217U;
    Fu_HallSensCalc_DWork.HallTickOld[6] = 16777217U;
    HallCalc_TickDifAry[0] = 16777217U;
    HallCalc_TickDifAry[1] = 16777217U;
    HallCalc_TickDifAry[2] = 16777217U;
    HallCalc_TickDifAry[3] = 16777217U;
    HallCalc_TickDifAry[4] = 16777217U;
    HallCalc_TickDifAry[5] = 16777217U;
    HallCalc_TickDifAry[6] = 16777217U;
  }
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
